#include <iostream>
#include <cstdlib>
#include "list.h"

using namespace std;

element* list::FirstEl = NULL;	  //This initialises the static element* 'FirstEl' to NULL
element* list::LastEl = NULL;	  //This initialises the static element* 'LastEl' to NULL
int data = 0;

element* list::NewElement(){
	element* currentel;					  //This creates a new pointer to element. This will hold the address of the new element
	if (FirstEl == NULL){					 //This checks whether FirstEl is equal to NULL
		currentel = new element;	 //This creates the new element and makes currentel equal to it
		FirstEl = currentel;			   //This makes FirstEl equal to the new element
		LastEl = currentel;			   //This makes LastEl equal to the new element
		currentel->nextel =  NULL;   //This makes the new elements, next element equal to NULL
		currentel->prevel = NULL;	//This makes the new elements, previous element equal to NULL
		currentel->data = data;
		data++;
		return currentel;
	}
	if (FirstEl != NULL){					 //This checks whether FirstEl is not equal to NULL
		currentel = new element;	//This creates the new element and makes currentel equal to it
		currentel->nextel = NULL;   //This makes the new elements, next element equal to NULL
		LastEl->nextel = currentel;  //This makes the last elements (in the list), next element equal to the new element
		currentel->prevel = LastEl;  //This makes the new elements, previous element equal to the last element in the list
		LastEl = currentel;			  //This makes LastEl equal to the new element
		currentel->data = data;
		data++;
		return currentel;
	}
	return NULL;
}

element* list::SearchElement(int searchitem){  //This traverses through the list, and is realy very simple so doesn't need explaining
	element* currentel;
	if (FirstEl == NULL){
		//system("CLS");
		cout << "There are no elements in the list\n";
		return NULL;
	}
	if (FirstEl != NULL){
		currentel = FirstEl;
		while (currentel != NULL){
			if (currentel->data == searchitem){
				return currentel;
			}
			else
				currentel = currentel->nextel;
		}
	}
	//system("CLS");
	cout << searchitem << " was not found in the list\n";
	return NULL;
}

element* list::DeleteElement(int searchitem){					//This function deletes an element anywhere within the list
	element* deleteptr;
	deleteptr = SearchElement(searchitem);

	if (deleteptr == FirstEl && deleteptr == LastEl){		 //This checks the element to be deleted is equal to the fist and last element
		FirstEl = NULL;												//This makes FirstEl equal to NULL
		LastEl = NULL;												//This makes LastEl equal to NULL
		delete deleteptr;											 //This deletes the element from the list
		return NULL;
	}
	if (deleteptr == FirstEl && deleteptr != LastEl){		  //This checks the element to be deleted is equal to the fist but notlast element
		FirstEl = deleteptr->nextel;							  //This makes FirstEl equal to the element to be deleted, next element
		deleteptr->nextel->prevel = NULL;				   //This makes t next element, previous element equal to NULL
		delete deleteptr;											 //This deletes the element from the list
		return NULL;
	}
	if (deleteptr != FirstEl && deleteptr == LastEl){		 //This checks the element to be deleted is equal to the last but notfist element
		LastEl = deleteptr->prevel;							 //This makes LastEl equal to the element to be deleted, previous element
		deleteptr->prevel->nextel = NULL;				  //This makes t previous element, next element equal to NULL
		delete deleteptr;											//This deletes the element from the list
		return NULL;
	}
	if (deleteptr != FirstEl && deleteptr != LastEl){		   //This checks the element to be deleted is equal to the fist and last element
		deleteptr->prevel->nextel = deleteptr->nextel; //This statement links the previous element to the next element in the list
		deleteptr->nextel->prevel = deleteptr->prevel; //This statement links the next element to the previous element in the list
		delete deleteptr;											 //This deletes the element from the list
		return NULL;
	}

	if (deleteptr == NULL){
		//system("CLS");
		cout << "No such item found within the list to delete\n";
	}
	return NULL;
}


int main()
{
	cout << "running" << endl;
}

